package com.linxz.video.appui.convert;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.linxz.framework.base.AppActivityManager;
import com.linxz.framework.base.BaseMVPActivity;
import com.linxz.video.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Linxz
 * 创建日期：2020年06月21日 15:48
 * version：v4.5.4
 * 描述：
 */
public class EditConvertActivity extends BaseMVPActivity<EditConvertPresenter> implements EditConvertView {

    private String filePath;
    public final static int REQUEST_CODE=10000;
    private List<Bitmap> bitmaps=new ArrayList<>();
    private ImageView ivConvert;
    private RecyclerView rvConverts;
    private ImageAdapter adapter;

    private int wWidth,iWidth,tWidth;
    private int arrayWidth[];

    private LinearLayout linearLayout;
    private ImageView imageviewPreview,ivw_to2;
    private GestureDetector mGestureDetector;

    private RelativeLayout.LayoutParams lp;


    public static void launch(Activity activity,String filePath){
        Intent intent=new Intent(activity,EditConvertActivity.class);
        intent.putExtra("filePath",filePath);
        AppActivityManager.getInstance().goFoResult(activity,intent,REQUEST_CODE);
    }

    @Override
    protected EditConvertPresenter createPresenter() {
        return new EditConvertPresenter(this,lifecycleSubject);
    }

    @Override
    public void onGetBundle(Bundle bundle) {
        super.onGetBundle(bundle);
        filePath=getIntent().getStringExtra("filePath");
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.videos_act_edit_convert);
        initUI();
        initData();
    }

    @Override
    protected void initUI() {

        linearLayout=findViewById(R.id.linearLayout);
        ivConvert=findViewById(R.id.ivConvert);
        rvConverts=findViewById(R.id.rvConverts);
        adapter=new ImageAdapter(bitmaps);
        rvConverts.setLayoutManager(new LinearLayoutManager(activity));
        rvConverts.setAdapter(adapter);



        mGestureDetector = new GestureDetector(this,new CoverGestureListener());
    }

    @Override
    protected void initData() {

    }


    public void getVideoThumbnail(Uri uri) {
        Bitmap bitmap;
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            retriever.setDataSource(activity, uri);
            String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
            int seconds=Integer.valueOf(time)/1000;
            int timeS=Integer.valueOf(time)/10;
            for(int i=1;i<=10;i++){
                bitmap=retriever.getFrameAtTime(i*timeS*1000,MediaMetadataRetriever.OPTION_CLOSEST);
                bitmaps.add(bitmap);
                addImgView(bitmap);
                adapter.notifyDataSetChanged();
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
        } finally {
            try {
                retriever.release();
            } catch (RuntimeException e) {
                e.printStackTrace();
            }
        }
    }


    public void addImgView(Bitmap bitmap){
//        SquareImageView imageView=new SquareImageView(context);
//        LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//        lp.weight=1;
//        imageView.setLayoutParams(lp);
//        imageView.setImageBitmap(bitmap);
//        linearLayout.addView(imageView);
    }


    private class CoverGestureListener extends GestureDetector.SimpleOnGestureListener {
        public boolean onDown(MotionEvent e) {
            seekTo(e.getX());
            return true;
        }
        public boolean onScroll(MotionEvent e1, MotionEvent e2,float distanceX, float distanceY) {
            float f = e2.getX();
            seekTo(f);
            return super.onScroll(e1, e2, distanceX, distanceY);
        }
    }

    public void onWindowFocusChanged(boolean hasFocus) {
        iWidth=imageviewPreview.getWidth();
        iWidth=wWidth-iWidth;
        tWidth=iWidth/10;
        arrayWidth=new int[10];
        for(int i=0;i<10;i++){
            arrayWidth[i]=tWidth+(i*tWidth);
        }
        seekTo(0);
        super.onWindowFocusChanged(hasFocus);
    }

    private void seekTo(float paramFloat) {
        float f = paramFloat - this.imageviewPreview.getWidth() / 2;
        if(f<0){
            f=0;
        }if(f>iWidth){
            f=iWidth;
        }
        lp.leftMargin = ((int) f);
        for(int i=0;i<arrayWidth.length;i++){
            if(arrayWidth[i]>=f){
                imageviewPreview.setImageBitmap(bitmaps.get(i));
                ivw_to2.setImageBitmap(bitmaps.get(i));
                break;
            }
        }
        this.imageviewPreview.setLayoutParams(this.lp);
    }


    public class ImageAdapter extends BaseQuickAdapter<Bitmap, BaseViewHolder>{

        public ImageAdapter(@Nullable List<Bitmap> data) {
            super(R.layout.video_item_convert_edit_img, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, Bitmap item) {
            helper.setImageBitmap(R.id.iv,item);
        }
    }


}
